from apis.base import Base
from loguru import logger


class UserManagerApi(Base):

    # 构造方法
    def __init__(self):
        self.add_user_url = self.get_url('/admin/admin/create')
        self.update_user_url = self.get_url('/admin/admin/update')
        self.delete_user_url = self.get_url('/admin/admin/delete')
        self.search_user_url = self.get_url('/admin/admin/list?page=1&limit=20&sort=add_time&order=desc')

    # 新增管理员
    def add_user(self,username,password,**kwargs):
        # 1. 定义请求头
        user_data = {"username":username,"password":password}

        # 2. 调用接口
        if kwargs:
            logger.info("添加管理员的可选参数为:{}".format(kwargs))
            user_data.update(kwargs)

        return self.post(self.add_user_url,user_data)

    # 修改管理员
    def edit_user(self,id,username,password,**kwargs):
        user_data = {"id":id,"username":username,"password":password}

        if kwargs:
            user_data.update(kwargs)

        return self.post(self.update_user_url,user_data)

    # 删除管理员
    def delete_user(self,id,username,**kwargs):
        user_data = {"id":id,"username":username}

        if kwargs:
            user_data.update(kwargs)

        return self.post(self.delete_user_url,user_data)

    # 查询管理员
    def search_user(self):
        return self.get(self.search_user_url)
